package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.fragment.f1;
import com.example.myapplication.fragment.f2;
import com.example.myapplication.fragment.f3;
import com.example.myapplication.fragment.f4;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {


    private ViewPager viewPage;
    private View tvBottom;
    private RadioGroup rgMenu;
    private RadioButton chaneel;
    private RadioButton cmenu2;
    private RadioButton cmenu3;
    private RadioButton cmenu4;
    ViewPageAdapter adapter;
    List<Fragment>fragmentList=new ArrayList<>();
    String titles[];
    com.example.myapplication.fragment.f1 f1;
    com.example.myapplication.fragment.f2 f2;
    com.example.myapplication.fragment.f3 f3;
    com.example.myapplication.fragment.f4 f4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        viewPage.setAdapter(adapter);
        viewPage.setCurrentItem(0);
        //页面滑屏事件
        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //0为滑动前，1滑动中，2滑动后
                if(state==2){
                    switch (viewPage.getCurrentItem()){
                        case 0:
                            chaneel.setChecked(true);
                            break;
                        case 1:
                            cmenu2.setChecked(true);
                            break;
                        case 2:
                            cmenu3.setChecked(true);
                            break;
                        case 3:
                            cmenu4.setChecked(true);
                            break;
                    }
                }
            }
        });
    }
    /*
    初始化控件和数据
     */

    private void initView() {

        viewPage = (ViewPager) findViewById(R.id.viewPage);
        tvBottom = (View) findViewById(R.id.tv_bottom);
        rgMenu = (RadioGroup) findViewById(R.id.rg_menu);
        chaneel = (RadioButton) findViewById(R.id.chaneel);
        cmenu2 = (RadioButton) findViewById(R.id.cmenu2);
        cmenu3 = (RadioButton) findViewById(R.id.cmenu3);
        cmenu4 = (RadioButton) findViewById(R.id.cmenu4);
        rgMenu.setOnCheckedChangeListener(this);
        titles=new String[]{"功能界面","信息","收藏","我的"};
        f1= com.example.myapplication.fragment.f1.getInstance(titles[0]);
        f2= com.example.myapplication.fragment.f2.getInstance(titles[1]);
        f3= com.example.myapplication.fragment.f3.getInstance(titles[2]);
        f4= com.example.myapplication.fragment.f4.getInstance(titles[3]);
        fragmentList.add(f1);
        fragmentList.add(f2);
        fragmentList.add(f3);
        fragmentList.add(f4);
        adapter=new ViewPageAdapter(getSupportFragmentManager(),fragmentList);
    }

/*
实现点击切换
 */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.chaneel:
                viewPage.setCurrentItem(0);
                break;
            case R.id.cmenu2:
                viewPage.setCurrentItem(1);
                break;
            case R.id.cmenu3:
                viewPage.setCurrentItem(2);
                break;
            case R.id.cmenu4:
                viewPage.setCurrentItem(3);
                break;
        }
    }
}